<?php

namespace app\index\controller;
use app\index\Controller;
use think\Db;
use think\Validate;
use think\Config;
/**
 * @title message（模块10）
 * @description 接口说明
 * @group 留言
 * @header name:model_id   default:10 desc:模块id
 * @groupremark 已使用错误码： 1100001,1100002,1100003
 */
class Message extends  Controller{
    /**
     * @title 添加留言
     * @description 接口说明
     * @author 梁敏
     * @url /index/message/add
     * @method ~
     *
     *
     * @param name:token type:string require:1
     *@param name:content type:string require:1
     * @return error_id: 1100001 校验错误,1100002 留言失败,
     */
    public function add() {
        //接口代码
        $data=input();

        $data['user_id']=UID;
        $vmsg = [
            'token.require' => '缺少token',
            'content.require'     => '内容不能为空',
            'user_id.require'   => '用户未授权',
        ];
        $validate = new Validate([
            'token'  => 'require',
            'content'=>"require",
            'user_id'=>"require",
        ],$vmsg);

        if (!$validate->check($data)) {
            $msg['error_id']="1100001";
            $msg['message']=$validate->getError();
            return $this->error($msg);
        }
        $re=Db::name("message")->insertGetId($data);
        if($re){
            $msg['message']="留言成功";
            return $this->success($msg);
        }else{
            $msg['error_id']="1100002";
            $msg['message']="留言失败";
            return $this->error($msg);
        }
    }
    /**
     * @title 留言列表
     * @description 接口说明
     * @author 梁敏
     * @url /index/message/lists
     * @method ~
     *
     *
     * @param name:token type:string require:1
     * @param name:page type:string norequire:1
     * @return error_id: 1100003校验失败,1100004获取失败,
     */
    public function lists() {
        //接口代码
        $data=input();

        $data['user_id']=UID;
        $vmsg = [
            'token.require' => '缺少token',
            'user_id.require'   => '用户未授权',
        ];
        $validate = new Validate([
            'token'  => 'require',
            'user_id'=>"require",
        ],$vmsg);

        if (!$validate->check($data)) {
            $msg['error_id']="1100003";
            $msg['message']=$validate->getError();
            return $this->error($msg);
        }

        $map['user_id']=$data['user_id'];
        $listRows = $this->request->param('numPerPage') ? : Config::get("paginate.list_rows");
        $list = Db::name('message')
            ->alias('m')
            ->join("tp_admin_user u","u.id=m.user_id","left")
            ->field("m.*")
            ->where($map)
            ->order("m.id desc")
            ->paginate($listRows, false, ['query' => $this->request->get(),"page"=>input("page")]);

        if($list){
            $msg['list']=$list->items();
            $msg['total']=$list->total();
            $msg['per_page']=$list->listRows();
            $msg['current_page']=$list->currentPage();
            $msg['message']="获取成功";
            return $this->success($msg);
        }else{
            $msg['error_id']="1100004";
            $msg['message']="获取失败";
            return $this->error($msg);
        }
    }
    /**
     * @title 留言内容
     * @description 接口说明
     * @author 梁敏
     * @url /index/message/info
     * @method ~
     * @param name:token type:string require:1
     * @param name:id type:int require:1
     * @return error_id: 1100005校验失败，1100006获取失败
     */
    public function info() {
        //接口代码
        $data=input();

        $data['user_id']=UID;
        $vmsg = [
            'token.require' => '缺少token',
            'user_id.require'   => '用户未授权',
            'id.require'   => '缺少留言id',
        ];
        $validate = new Validate([
            'token'  => 'require',
            'user_id'=>"require",
            'id'=>"require",
        ],$vmsg);

        if (!$validate->check($data)) {
            $msg['error_id']="1100005";
            $msg['message']=$validate->getError();
            return $this->error($msg);
        }

        $map['m.id']=$data['id'];
        $conten = Db::name('message')
            ->alias('m')
            ->join("tp_admin_user u","u.id=m.user_id","left")
            ->field("m.*")
            ->where($map)
            ->find();

        if($conten){
            $msg['data']=$conten;
            $msg['message']="获取成功";
            return $this->success($msg);
        }else{
            $msg['error_id']="1100006";
            $msg['message']="获取失败11";
            return $this->error($msg);
        }
    }
}
